<template>
  <div>
    <div class="search">
      <el-form label-width="100px" :inline="true">
        <el-form-item
          label="一体杆名称"
        >
          <el-input v-model="params.poleName" />
        </el-form-item>
        <el-form-item
          label="一体杆编号"
        >
          <el-input v-model="params.poleNumber" />
        </el-form-item>
        <el-form-item
          label="运行状态"
        >
          <el-select v-model="params.poleStatus">
            <el-option label="全部" value="" />
            <el-option label="正常" value="0" />
            <el-option label="异常" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="getRodList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button type="primary" @click="dialogVisible = true">添加一体杆</el-button>
      <el-button type="danger" @click="del(ids)">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="rodList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column
          type="index"
          label="序号"
        />
        <el-table-column
          label="一体杆名称"
          width="180"
          prop="poleName"
        />
        <el-table-column
          label="一体杆编号"
          prop="poleNumber"
        />
        <el-table-column
          label="一体杆IP"
          prop="poleIp"
        />
        <el-table-column
          label="安装区域"
          prop="areaName"
        />
        <el-table-column
          label="一体杆类型"
          prop="areaId"
        >
          <template #default="{row}">
            {{ formatPoleType(row.poleType) }}
          </template>
        </el-table-column>

        <el-table-column
          label="运行状态"
          prop="poleType"
        >
          <template #default="{row}">
            {{ row.poleStatus === 0 ? '正常':'异常' }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template #default="{row}">
            <el-button
              size="mini"
              type="text"
              @click="edit(row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="del(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="footer">
      <div class="page-container">
        <el-pagination
          layout="total, prev, pager, next"
          :total="total"
          :page-size="params.pageSize"
          :current-page="params.page"
          @current-change="pageChange"
        />
      </div>
    </div>
    <!-- 添加表单弹层 -->
    <el-dialog
      :title="id?'编辑一体杆':'添加一体杆'"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 添加表单 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input v-model="addForm.poleName" />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input v-model="addForm.poleNumber" />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input v-model="addForm.poleIp" />
          </el-form-item>
          <el-form-item label="关联区域" prop="areaId">
            <el-select v-model="addForm.areaId" placeholder="请选择关联区域">
              <el-option v-for="area in areaList" :key="area.areaId" :label="area.areaName" :value="area.areaId" />
            </el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType">
            <el-select v-model="addForm.poleType" placeholder="请选择一体杆类型">
              <el-option label="出口" value="export" />
              <el-option label="入口" value="entrance" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>
  </div>

</template>
<script>
import { getRodListAPI, addRodAPI, editRodAPI, delRodAPI, getAreaListAPI } from '@/api/rod'
export default {
  name: 'Rod',
  data() {
    return {
      rodList: [],
      areaList: [],
      params: {
        page: 1,
        pageSize: 10,
        poleName: '',
        poleNumber: '',
        poleStatus: ''
      },
      total: 0,
      // 控制表单弹层的显示隐藏
      dialogVisible: false,
      // 存储id，用于区分操作
      id: '',
      // 用于批量删除
      ids: '',
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      },
      addFormRules: {
        poleName: [
          { required: true, message: '请输入一体杆名称', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '请输入一体杆编号', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入一体杆IP', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '请输入关联区域', trigger: 'blur' }
        ],
        poleType: [
          { required: true, message: '请输入一体杆类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getRodList()
  },
  methods: {
    // 获取一体杆列表
    async getRodList() {
      const res = await getRodListAPI(this.params)
      this.rodList = res.data.rows
      console.log(this.rodList)
      this.total = res.data.total
    },
    // 格式化一体杆类型
    formatPoleType(type) {
      const poleType = {
        'entrance': '入口',
        'export': '出口',
        'center': '?口'
      }
      return poleType[type]
    },
    // 页码被修改
    pageChange(page) {
      this.params.page = page
      // 重新请求数据刷新页面
      this.getRodList()
    },
    // 关闭表单弹层
    closeDialog() {
      this.dialogVisible = false
      // 重置表单
      this.id = ''
      this.addForm = {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      }
      this.$refs.addForm.resetFields()
    },
    // 打开表单弹层
    async openDialog() {
      // 获取关联区域下拉列表数据
      const res = await getAreaListAPI()
      this.areaList = res.data
    },
    // 表单提交
    confirmAdd() {
      // 校验表单
      this.$refs.addForm.validate(async valid => {
        // 通过
        if (valid) {
          // 修改
          if (this.id) {
            await editRodAPI(this.addForm)
          } else {
            // 添加
            await addRodAPI(this.addForm)
          }
          this.$message.success(`${this.id ? '修改' : '添加'}成功`)
          this.dialogVisible = false
          this.getRodList()
        }
      })
    },
    // 编辑
    edit(id) {
      this.id = id
      this.dialogVisible = true
      // 获取要修改的数据，剔除接口不需要的字段，回填到表单
      const item = this.rodList.find(item => item.id === id)
      console.log(11, item)
      delete item.areaName
      delete item.poleStatus
      this.addForm = { ...item }
    },
    // 删除
    del(ids) {
      // console.log(ids)
      if (!ids) return this.$message.error('请选择要删除的数据')
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送删除请求
        await delRodAPI(ids)
        // 刷新列表
        this.getRodList()
        // 成功提示
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(() => {})
    },
    // 复选框状态发生改变时触发
    handleSelectionChange(selection) {
      // 获取所有被选中的数据，提取id处理成接口要求的格式，用于批量删除
      this.ids = selection.map(item => {
        return item.id
      }).join(',')
    }
  }
}
</script>
<style lang="scss" scoped>
.search ::v-deep .el-input__inner{
  height: 30px;
  width: 200px;
}
.search ::v-deep .el-button--primary{
  font-size: 14px;
}
.footer ::v-deep .page-container{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>
